﻿@using Webdiyer.WebControls.Mvc
@model List<Alabama.TongHopDetail>
@{
               
    var lstNguoiTruc = Alabama.DB.Entities.NguoiTruc.ToList();
    var listTongHop = (List<Alabama.TongHop>)ViewBag.ListTongHop;
}
<style type="text/css"> 
    select
    {
        min-width:100px;
        
    }
    .row-fluid [class*="span"] {
        margin-left:0;
    }

    .TieuDeEdit
    {
        position:absolute;
        bottom:0;
        left:50%;
        margin-left:-40px;
        word-break: break-all;
    }
</style>
<script type="text/javascript">
    $(document).ready(function () {
        //        $('.StartDate').datepicker({ language: "vi", format: "dd/mm/yyyy" }).on('changeDate', function (ev) {
        //            var curr = new Date(convertDateTimeVItoEN($(this).val()));
        //            var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
        //            var res;
        //            if (first + 1 != curr.getDate()) {
        //                res = '';
        //                $(".TongHop").hide();
        //                return false;
        //            } else {
        //                var last = first + 7; // last day is the first day + 6

        //                var firstday = new Date(curr.setDate(first));

        //                var lastday = new Date(curr.setDate(last));
        //                res = lastday.getDate() + '/' + (lastday.getMonth() + 1) + '/' + lastday.getFullYear();
        //                $(".TongHop").show();
        //                $('.EndDate').val(res);
        //                return true;
        //            }
        //        });
        
        loadSelectDay();      
        $(".selectday").change(function () {
            var m = eval($(".selectmonth").val());
            var y = eval($(".selectyear").val());
            var d = eval($(this).val());
            var fromdate = new Date(y, m, d, 0, 0, 0, 0);
            var enddate = fromdate.addDays(6).toLocaleDateString();
            var list = enddate.split("/");
            $('.EndDate').val(list[1]+"/"+list[0]+"/"+list[2]);
            resetURL();                 
        });
        $(".selectmonth").change(function () {
            var m = eval($(this).val());
            var y = eval($(".selectyear").val());
            var mondays = getMondaysByMonthYear(m, y);
            $('.selectday').html('');
            $.each(mondays, function (key, value) {
                $('.selectday')
                     .append($("<option></option>")
                     .attr("value", value)
                     .text(value));
            });
            var enddate = mondays[0] + 6 + "-" + (m + 1) + "-" + y;
            $('.EndDate').val(enddate);
            resetURL();
        });
        $(".selectyear").change(function () {
            var m = eval($(".selectmonth").val());
            var y = eval($(".selectyear").val());
            var mondays = getMondaysByMonthYear(m, y);
            $('.selectday').html('');
            $.each(mondays, function (key, value) {
                $('.selectday')
                     .append($("<option></option>")
                     .attr("value", value)
                     .text(value));
            });
            var enddate = mondays[0] + 6 + "-" + (m + 1) + "-" + y;
            $('.EndDate').val(enddate);
            resetURL();
        });
        $(".TongHop").click(function () {
            var start = $(".selectday").val() + "/" + (eval($(".selectmonth").val())+1) + "/" + $(".selectyear").val();
            var end = $(".EndDate").val();
            var url = $(this).attr('href');
            var newUrl = addParam(addParam(url, "fromDate", start), "endDate", end);
            $(this).attr('href', newUrl);
        });
        $(".EditDetail").click(function () {
            var id = $(this).attr('value');
            $.ajax({
                url: "/TongHopLichCongTac/EditDetail",
                type: "GET",
                data: { "id": id, "url": "@Request.RawUrl" },
                success: function (model) {
                    $("#Modal-EditDetail").html(model);
                }
            });
        });
        $(".ChangeNguoiTruc").change(function () {
            var idNguoiTruc = $(this).val();
            var id = $(this).attr('TongHopID');
            var url = "@Request.RawUrl";
            $.ajax({
                type: "GET",
                url: "/TongHopLichCongTac/ChangeNguoiTruc",
                data: { "id": id, "idNguoiTruc": idNguoiTruc },
                success: function (value) {
                    if (value == "True") {
                        alert("Cập nhật thành công");
                        //location.href = url;
                    }
                }
            });
        });

//        // sửa tiêu đề
//        $(".TieuDe").hover(function () {
//            $(".TieuDeEdit").show();
//        }, function () {
//            $(".TieuDeEdit").hide();
//        });
        $(".TieuDeEdit").click(function () {
            var id = $(this).attr('value');
            $.ajax({
                url: "/TongHopLichCongTac/EditTieuDe",
                type: "GET",
                data: { "id": id, "url": "@Request.RawUrl" },
                success: function (model) {
                    $("#Modal-EditDetail").html(model);
                }
            });
        });

    });
    function resetURL() {
        var d1 = $(".selectday").val();
        var m1 = eval($(".selectmonth").val())+1;
        var y1 = $(".selectyear").val();
        var end1 = $(".EndDate").val();
        var url1 = "@Url.Action("NewOrEdit", "TongHopLichCongTac")";
        var newUrl = addParam(addParam(url1, "endDate",end1), "fromDate", d1+"/"+m1+"/"+y1);
        location.href = newUrl;
}
    function loadSelectDay() {
        var m = eval($('.selectmonth').val());
        var y = eval($(".selectyear").val());
        var fromdate = "@Request.QueryString["fromDate"]";
        var enddate = "@Request.QueryString["endDate"]";
        var selected = 0;
        if(fromdate!=""){
            var arr = fromdate.split("/");
            m=eval(arr[1])-1;
            y=eval(arr[2]);
            selected = eval(arr[0]);
            $('.selectmonth').val(m);   
        } 
        var mondays = getMondaysByMonthYear(m, y);
        $('.selectday').html('')
        $.each(mondays, function (key, value) {
            if (selected!=0 && selected==value) {
                $('.selectday')
                     .append($("<option selected='selected'></option>")
                     .attr("value", value)
                     .text(value));
            }else {
                $('.selectday')
                     .append($("<option></option>")
                     .attr("value", value)
                     .text(value));
            }
        });
        if(enddate==""){
            enddate = mondays[0] + 6 + "-" + (m + 1) + "-" + y;  
        }
        $('.EndDate').val(enddate);
    }
</script>
<h3>
    Tổng hợp lịch công tác</h3>
<div class="row-fluid">
    <div class="row-fluid">
        <div class="span12">
            @if (!(ViewBag.IsEdit != null && (bool)ViewBag.IsEdit))
            {
                if (ViewBag.TongHopLichCongTac != null && (bool)ViewBag.TongHopLichCongTac)
                {
                @Html.ActionLink("Tổng hợp", "TongHop", null, new { @class = "btn btn-info TongHop" })
                }
            }
            else
            {
                if (ViewBag.TongHopLaiLichCongTac != null && (bool)ViewBag.TongHopLaiLichCongTac)
                {
                @Html.ActionLink("Tổng hợp lại", "TongHopLai", null, new { @class = "btn btn-info TongHop" })   
                }
            }
            @if (Model.Count > 0)
            {
                <a href="/TongHopLichCongTac/DisplayReport?codedate=@ViewBag.CodeDate" target="_blank" class="btn btn-info">
                    <img src="../../Content/img/pdf.jpg" style="height: 18px" />
                    Xuất PDF</a>
                <a href="/TongHopLichCongTac/DisplayReport?codedate=@ViewBag.CodeDate&&type=1" target="_blank" class="btn btn-info">
                    <img src="../../Content/img/word.png" style="height: 18px" />
                    Xuất Word</a>
            }
            <a href="@Url.Action("Index")" class="btn"><i class="icon-mail-reply"></i>&nbsp;Quay
                lại</a>
        </div>
    </div>
    <br />
    <div class="row-fluid">
        <div class="span33">
            <label>
                Tuần từ ngày</label>
            @*<input type="text" name="name" value="@Request.QueryString["fromDate"]" class="StartDate" />            *@
            <select class="selectday">
            </select>
        </div>
        <div class="span33">
            <label>
                Tháng</label>
            <select class="selectmonth">
                @for (int i = 0; i < 12; i++)
                {
                    string selected = (DateTime.Now.Month == i + 1) ? "selected='selected'" : "";
                    <option value="@i" @selected >@(i + 1)</option>
                }
            </select>
        </div>
        <div class="span33">
            <label>
                Năm</label>
            <select class="selectyear">
                @for (int i = 2014; i < DateTime.Now.Year + 2; i++)
                {
                    <option value="@i">@i</option>
                }
            </select>
        </div>
        <div class="span33">
            <label>
                Ngày cuối tuần</label>
            <input type="text" name="name" value="@Request.QueryString["EndDate"]" class="EndDate" readonly="readonly" />
        </div>
    </div>
    <div class="row-fluid">
        <div class="span12" style="text-align: center; position: relative; padding-bottom: 5px;
            margin-bottom: 5px;">
            @if (listTongHop != null && listTongHop.Count > 0)
            {
                <h3 class="TieuDe">@(Html.Raw(listTongHop[0].TieuDe.Title.Replace("\n", "<br />")))
                </h3>
                               if (ViewBag.SuaXoaLichCongTac != null && (bool)ViewBag.SuaXoaLichCongTac)
                               {
                <a href="#Modal-EditDetail" role="button" class="TieuDeEdit" value="@listTongHop[0].TieuDeID"
                data-toggle="modal">Sửa tiêu đề</a>
                               }
            }
        </div>
    </div>
    <table class="table table-bordered">
        <tr>
            <th style="width: 6%">
            </th>
            <th style="width: 3%">
                Thời gian
            </th>
            <th>
                Nội dung
            </th>
            <th>
                Người thực hiện
            </th>
            <th style="width: 15%">
                Địa điểm
            </th>
            <th style="width: 15%">
                Trực lãnh đạo
            </th>
            <th>
            </th>
        </tr>
        @if (Model != null && Model.Count > 0)
        {
            foreach (var item in listTongHop)
            {
                var list = Model.Where(m => m.TongHopID == item.ID).OrderBy(n => n.Time).ToList();
               
            <tr>
                @if (item.DayOfWeek == 6)
                {
                    if (list.Count > 0)
                    {
                    <td rowspan="@(list.Count + 1)">
                        CN
                    </td>
                    }
                    else
                    {
                    <td rowspan="1">
                        CN
                    </td>
                    }

                }
                else
                {
                    if (list.Count > 0)
                    {
                    <td rowspan="@(list.Count + 1)">
                        Thứ @(item.DayOfWeek + 2)
                    </td>
                    }
                    else
                    {
                    <td rowspan="1">
                        Thứ @(item.DayOfWeek + 2)
                    </td>
                    }

                }
                <td colspan="4">
                </td>
                <td style="border-left: none;">
                    @if (ViewBag.SuaXoaLichCongTac != null && (bool)ViewBag.SuaXoaLichCongTac)
                    {
                        <select class="ChangeNguoiTruc span12" TongHopID="@item.ID">
                            <option value="0">--</option>
                            @foreach (var item12 in lstNguoiTruc)
                            {
                                var selected = "";
                                if (item12.ID == item.NguoiTrucID)
                                {
                                    selected = "selected='selected'";
                                }
                                <option value="@item12.ID" @selected>@item12.Title</option>
                            }
                        </select>
                    }
                    else
                    {
                        @(item.NguoiTruc != null ? item.NguoiTruc.Title : "")
                    }
                </td>
                <td style="border-left: none;">
                </td>
            </tr>
            
                    foreach (var item1 in list)
                    {
            <tr>
                <td>
                    @item1.Time.Hours:@item1.Time.Minutes
                </td>
                <td>
                    @item1.NoiDung
                </td>
                <td>
                    @(Html.Raw((item1.NguoiThucHien + "").Replace("\n", "<br />")))
                </td>
                <td>
                    @(item1.Location)
                </td>
                <td>
                    @(item.NguoiTruc != null ? item.NguoiTruc.Title : "")
                </td>
                <td style="width: 3%">
                    @if (ViewBag.SuaXoaLichCongTac != null && (bool)ViewBag.SuaXoaLichCongTac)
                    {
                        <a href="#Modal-EditDetail" role="button" class="EditDetail" value="@item1.ID" data-toggle="modal">
                            Sửa</a><br />
                        <a href="@Url.Action("DeleteDetail", "TongHopLichCongTac", new { id = item1.ID, url = Request.RawUrl })" class="confirmDelete error">
                            Xóa</a>
                    }
                </td>
            </tr>                                                                                      
                    }

            }
        }
    </table>
</div>
@*<div id="Modal-EditDetail" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1"
    aria-hidden="true">
</div>*@
<div id="Modal-EditDetail" class="modal hide fade">
</div>
